package com.fawvwebanma.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fawvwebanma.dao.UserDao;
import com.fawvwebanma.dto.ResultData;
import com.fawvwebanma.entity.User;
import com.fawvwebanma.mapper.UserMapper;
import com.fawvwebanma.service.IUserService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jieye.ren
 * @date 2021/3/11
 */
@Component
@DubboService
@Slf4j
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserDao userDao;

    @Override
    public ResultData<User> insertUser(User user) {
        try {
            userDao.saveUser(user);
            return ResultData.success(user);
        } catch (Exception e) {
            return ResultData.error("0", "网络延迟，稍后再试......");
        }
    }

    @Override
    public ResultData<List<User>> selectAllUser() {
        try {
            List<User> users = userDao.getAllUsers();
            return ResultData.success(users);
        } catch (Exception e) {
            return ResultData.error("0", "网络延迟，稍后再试......");
        }
    }

    @Override
    public ResultData<User> updateUser(User user) {
        // 需要先判断用户是否存在
        try {
            userDao.updateUserById(user);
            return ResultData.success(user);
        } catch (Exception e) {
            return ResultData.error("0", "网络延迟，稍后再试......");
        }
    }

    @Override
    public ResultData<User> deleteUser(User user) {
        // 需要先判断用户是否存在
        try {
            userDao.deleteUserById(user);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.error("0", "网络延迟，稍后再试......");
        }
    }
}
